home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / BCHDRN.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  16KB  |  559 lines

  1. /* -*- C -*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/bchdrn.c,v 1.4 1992/03/26 04:21:11 cph Exp $
  4.  
  5. Copyright (c) 1991-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Drone program for overlapped I/O in bchscheme. */
  36.  
  37. #include <setjmp.h>
  38. #include "ux.h"
  39. #include "bchdrn.h"
  40.  
  41. #define DEBUG
  42. /* #define DEBUG_1 */
  43. /* #define DEBUG_2 */
  44.  
  45. extern char * EXFUN (error_name, (int));
  46. extern int EXFUN (retrying_file_operation,
  47.           (/* no prototype because (CONST char *) != (char *) */
  48.            int EXFUN((*), ()),
  49.            int, char *, long, long, char *, char *, long *,
  50.            int EXFUN((*), (char *, char *))));
  51.  
  52. #ifdef HAVE_SYSV_SHARED_MEMORY
  53.  
  54. static struct
  55. {
  56.   char * program_name;
  57.   char * file_name;        /* gc file name */
  58.   int shmid;            /* shared memory descriptor */
  59.   int tdron;            /* total number of drones */
  60.   int nbuf;            /* total number of buffers */
  61.   long bufsiz;            /* size of each buffer in bytes */
  62.   int sdron;            /* index of first drone to start */
  63.   int ndron;            /* number of drones to start */
  64.   int keep_p;            /* keep the gc file if Scheme dies? */
  65. } arguments;
  66.  
  67. struct argdesc
  68. {
  69.   char * name;
  70.   char * format;
  71.   PTR location;
  72. };
  73.  
  74. static char string_a[] = "%s";
  75. #define STRING_FMT &string_a[0]
  76.  
  77. static char decimal_int_a[] = "%d";
  78. #define DECIMAL_INT_FMT &decimal_int_a[0]
  79.  
  80. static char decimal_long_a[] = "%ld";
  81. #define DECIMAL_LONG_FMT &decimal_long_a[0]
  82.  
  83. static struct argdesc command_line[] =
  84. {
  85.   { "program_name",    STRING_FMT,        &arguments.program_name },
  86.   { "file_name",    STRING_FMT,        &arguments.file_name },
  87.   { "shmid",        DECIMAL_INT_FMT,    &arguments.shmid },
  88.   { "tdron",        DECIMAL_INT_FMT,    &arguments.tdron },
  89.   { "nbuf",        DECIMAL_INT_FMT,    &arguments.nbuf },
  90.   { "bufsiz",        DECIMAL_LONG_FMT,    &arguments.bufsiz },
  91.   { "sdron",        DECIMAL_INT_FMT,    &arguments.sdron },
  92.   { "ndron",        DECIMAL_INT_FMT,    &arguments.ndron },
  93.   { "keep_gc_file",    DECIMAL_INT_FMT,    &arguments.keep_p }
  94. };
  95.  
  96. static int gc_fid = -1;
  97. static char * shared_memory;
  98. static struct buffer_info * gc_buffers;
  99. static struct drone_info * myself;
  100. static unsigned long * drone_version, * wait_mask;
  101. static jmp_buf abort_point;
  102. static pid_t boss_pid;
  103.  
  104. static void 
  105. DEFUN (posix_signal, (signum, handler),
  106.        int signum AND void EXFUN ((*handler), ()))
  107. {
  108.   static void EXFUN (shutdown, ());
  109.   struct sigaction new;
  110.  
  111.   new.sa_handler = handler;
  112.   UX_sigemptyset (&new.sa_mask);
  113.   UX_sigaddset ((&new.sa_mask), SIGCONT);
  114.   UX_sigaddset ((&new.sa_mask), SIGQUIT);
  115.   new.sa_flags = SA_NOCLDSTOP;
  116.   
  117.   if ((UX_sigaction (signum, &new, 0)) == -1)
  118.   {
  119.     fprintf (stderr, "%s (%d, posix_signal): sigaction failed. errno = %s.\n",
  120.          arguments.program_name, myself->index, (error_name (errno)));
  121.     fflush (stderr);
  122.     shutdown (0);
  123.     /*NOTREACHED*/
  124.   }
  125.   return;
  126. }
  127.  
  128. static void
  129. DEFUN (shutdown, (sig), int sig)
  130. {
  131.   myself->state = drone_dead;
  132.   if (gc_fid != -1)
  133.     close (gc_fid);
  134.   shmdt (shared_memory);
  135.   if (sig == -1)
  136.   {
  137.     shmctl (arguments.shmid, IPC_RMID, ((struct shmid_ds *) 0));
  138.     if (!arguments.keep_p)
  139.       unlink (arguments.file_name);
  140.   }
  141.   exit (0);
  142.   /*NOTREACHED*/
  143. }
  144.  
  145. static void
  146. DEFUN (abort_operation, (sig), int sig)
  147. {
  148.   RE_INSTALL_HANDLER (SIGQUIT, abort_operation);
  149.   myself->state = drone_aborting;
  150.   longjmp (abort_point, 1);
  151.   /*NOTREACHED*/
  152. }
  153.  
  154. static void
  155. DEFUN (continue_running, (sig), int sig)
  156. {
  157.   RE_INSTALL_HANDLER (SIGCONT, continue_running);
  158.   longjmp (abort_point, 1);
  159.   /*NOTREACHED*/
  160. }
  161.  
  162. static int
  163. DEFUN (always_one, (operation_name, noise),
  164.        char * operation_name AND char * noise)
  165. {
  166.   return (1);
  167. }
  168.  
  169. static void
  170. DEFUN (process_requests, (drone), struct drone_info * drone)
  171. {
  172. #if !(defined(_HPUX) && (_HPUX_VERSION >= 80))
  173.   extern int EXFUN (select, (int, int *, int *, int *, struct timeval *));
  174. #endif
  175.   sigset_t non_blocking_signal_mask, blocking_signal_mask;
  176.   int result, count, buffer_index, flags;
  177.   long current_position = -1;
  178.   struct timeval timeout;
  179.   struct stat file_info;
  180.   unsigned long read_mask, my_mask;
  181.  
  182.   myself = drone;
  183.   my_mask = (((unsigned long) 1) << drone->index);
  184.   drone->DRONE_PID = (getpid ());
  185.   gc_fid = (open (arguments.file_name, O_RDWR, 0644));
  186.   if (gc_fid == -1)
  187.   {
  188.     fprintf (stderr,
  189.          "%s (%d, process_requests): open failed. errno = %s.\n",    
  190.          arguments.program_name, drone->index, (error_name (errno)));
  191.     fflush (stderr);
  192.     if (drone->DRONE_PPID == boss_pid)
  193.       (void) (kill (boss_pid, SIGCONT));
  194.     shutdown (0);
  195.     /*NOTREACHED*/
  196.   }
  197. #ifdef DEBUG_1
  198.   printf ("%s (%d, process_requests): Starting (pid = %d, ppid = %d).\n",
  199.       arguments.program_name, drone->index,
  200.       drone->DRONE_PID, drone->DRONE_PPID);
  201.   fflush (stdout);
  202. #endif
  203.   if ((result = (fstat (gc_fid, &file_info))) == -1)
  204.   {
  205.     fprintf (stderr,
  206.          "%s (%d, process_requests): fstat failed. errno = %s.\n",
  207.          arguments.program_name, drone->index, (error_name (errno)));
  208.     fflush (stderr);
  209.   }
  210.   /* Force O_SYNC only if we are dealing with a raw device. */
  211.     
  212.   if ((result == -1) || ((file_info.st_mode & S_IFMT) == S_IFCHR))
  213.   {
  214.     if ((flags = (fcntl (gc_fid, F_GETFL, 0))) == -1)
  215.     {
  216.       fprintf
  217.     (stderr,
  218.      "%s (%d, process_requests): fcntl (F_GETFL) failed. errno = %s.\n",
  219.      arguments.program_name, drone->index, (error_name (errno)));
  220.       fflush (stderr);
  221.     }
  222.     else
  223.     {
  224.       flags |= O_SYNC;
  225.       if ((fcntl (gc_fid, F_SETFL, flags)) == -1)
  226.       {
  227.     fprintf
  228.       (stderr,
  229.        "%s (%d, process_requests): fcntl (F_SETFL) failed. errno = %s.\n",
  230.        arguments.program_name, drone->index, (error_name (errno)));
  231.     fflush (stderr);
  232.       }
  233.     }
  234.   }
  235.  
  236.   UX_sigemptyset (&non_blocking_signal_mask);
  237.   UX_sigemptyset (&blocking_signal_mask);
  238.   UX_sigaddset ((&blocking_signal_mask), SIGCONT);
  239.   UX_sigaddset ((&blocking_signal_mask), SIGQUIT);
  240.   UX_sigprocmask (SIG_SETMASK, (&blocking_signal_mask), 0);
  241.   posix_signal (SIGQUIT, abort_operation);
  242.   posix_signal (SIGCONT, continue_running);
  243.  
  244.   if ((setjmp (abort_point)) == 0)
  245.   {
  246.     count = drone->index; 
  247.     drone->state = drone_idle;
  248.     if (drone->DRONE_PPID == boss_pid)
  249.       (void) (kill (boss_pid, SIGCONT));
  250.   }
  251.   else
  252.     goto redo_dispatch;
  253.  
  254.   for (; 1; count++)
  255.   {
  256.     timeout.tv_sec = 6;
  257.     timeout.tv_usec = 0;
  258.  
  259.     UX_sigprocmask (SIG_SETMASK, (&non_blocking_signal_mask), 0);
  260.     result = (select (0, 0, 0, 0, &timeout));
  261.     UX_sigprocmask (SIG_SETMASK, (&blocking_signal_mask), 0);
  262.  
  263.     if ((drone->state != drone_idle)
  264.     || ((result == -1) && (errno == EINTR)))
  265.     {
  266.       if (result != -1)
  267.       {
  268.     fprintf (stderr,
  269.          "\n%s (%d, process_requests): request after timeout %d.\n",
  270.          arguments.program_name, drone->index, drone->state);
  271.     fflush (stderr);
  272.       }
  273. redo_dispatch:
  274.       switch (drone->state)
  275.       {
  276.     default:
  277.       fprintf (stderr,
  278.            "\n%s (%d, process_requests): Unknown/bad operation %d.\n",
  279.            arguments.program_name, drone->index, drone->state);
  280.       fflush (stderr);
  281.       shutdown (0);
  282.       /*NOTREACHED*/
  283.  
  284.     case drone_idle:
  285.       break;
  286.  
  287.     case drone_aborting:
  288. #ifdef DEBUG_1
  289.       printf ("\n%s (%d, process_requests): Aborting.",
  290.           arguments.program_name, drone->index);
  291.       fflush (stdout);
  292. #endif
  293.       drone->buffer_index = -1;
  294.       current_position = -1;
  295.       break;
  296.  
  297.     case drone_reading:
  298.     case drone_writing:
  299.     {
  300.       /* Can't use buffer->bottom because the shared memory may be
  301.          attached at a different address!
  302.        */
  303.  
  304.       int saved_errno;
  305.       enum drone_state operation;
  306.       char * operation_name, * buffer_address;
  307.       struct buffer_info * buffer;
  308.       struct gc_queue_entry * entry;
  309.  
  310.       operation = drone->state;
  311.       buffer_index = (drone->buffer_index);
  312.       buffer = (gc_buffers + buffer_index);
  313.  
  314.       entry = ((struct gc_queue_entry *)
  315.            (((char *) drone) + (drone->entry_offset)));
  316.       entry->error_code = 0;
  317.  
  318.       operation_name = ((operation == drone_reading) ? "read" : "write");
  319.       buffer_address = (shared_memory + (arguments.bufsiz * buffer_index));
  320. #ifdef DEBUG_1
  321.       printf ("\n%s (%d, process_requests %s): Buffer index = %d.\n",
  322.           arguments.program_name, drone->index, operation_name,
  323.           buffer_index);
  324.       printf ("\tBuffer address = 0x%lx; Position = 0x%lx; Size = 0x%lx.",
  325.           buffer_address, buffer->position, buffer->size);
  326.       fflush (stdout);
  327. #endif
  328.  
  329.       UX_sigprocmask (SIG_SETMASK, (&non_blocking_signal_mask), 0);
  330.       result = (retrying_file_operation
  331.             (((operation == drone_reading)
  332.               ? ((int (*) ()) read)
  333.               : ((int (*) ()) write)),
  334.              gc_fid, buffer_address,
  335.              buffer->position, buffer->size, operation_name, NULL,
  336.              ¤t_position, always_one));
  337.       saved_errno = errno;
  338.       UX_sigprocmask (SIG_SETMASK, (&blocking_signal_mask), 0);
  339.  
  340.       if (result == -1)
  341.       {
  342.         buffer->state = ((operation == drone_reading)
  343.                  ? buffer_read_error
  344.                  : buffer_write_error);
  345.         drone->buffer_index = -1;
  346.         entry->drone_index = -1;
  347.         entry->error_code = saved_errno;
  348.         entry->state = entry_error;
  349.         current_position = -1;
  350. #ifdef DEBUG
  351.         printf ("\n%s (%d, process_requests): %s error (errno = %s).\n",
  352.             arguments.program_name, drone->index, operation_name,
  353.             (error_name (saved_errno)));
  354.         fflush (stdout);
  355. #endif
  356.       }
  357.  
  358.       else
  359.       {
  360.         buffer->state = ((operation == drone_reading)
  361.                  ? buffer_ready
  362.                  : buffer_idle);
  363.         drone->buffer_index = -1;
  364.         entry->drone_index = -1;
  365.         if (operation == drone_writing)
  366.         {
  367.           entry->retry_count = 0;
  368.           entry->state = entry_idle;
  369.         }
  370.  
  371. #ifdef DEBUG_1
  372.       printf ("\n%s (%d, process_requests %s): Done.",
  373.           arguments.program_name, drone->index, operation_name);
  374.       fflush (stdout);
  375. #endif
  376.       }
  377.     }
  378.       }
  379.  
  380.       count = 0;
  381.       drone->state = drone_idle;
  382.       read_mask = (* wait_mask);
  383.       if ((read_mask & my_mask) == my_mask)
  384.     (void) (kill (boss_pid, SIGCONT));
  385.     }
  386.     else if (result == 0)
  387.     {
  388.       if (count == arguments.tdron)
  389.       {
  390.     count = 0;
  391.     if ((kill (boss_pid, 0)) == -1)
  392.       shutdown (-1);
  393.       }
  394.       read_mask = (* wait_mask);
  395.       if ((read_mask & my_mask) == my_mask)
  396.       {
  397.     fprintf (stderr,
  398.          "\n%s (%d, process_requests): signal deadlock (%s)!\n",
  399.          arguments.program_name, drone->index,
  400.          ((read_mask == ((unsigned long) -1)) ? "any" : "me"));
  401.     fflush (stderr);
  402.     drone->state = drone_idle; /* !! */
  403.     (void) (kill (boss_pid, SIGCONT));
  404.       }
  405.     }
  406.   }
  407. }
  408.  
  409. static void
  410. DEFUN_VOID (start_drones)
  411. {
  412.   pid_t my_pid;
  413.   int counter, cpid;
  414.   struct drone_info *gc_drones, *drone;
  415.  
  416.   my_pid = (getpid ());
  417.  
  418.   shared_memory = (shmat (arguments.shmid, ((char *) 0), 0));
  419.   if (shared_memory == ((char *) -1))
  420.   {
  421.     fprintf (stderr,
  422.          "\
  423. %s (start_drones): Unable to attach shared memory segment %d (errno = %s).\n",
  424.          arguments.program_name, arguments.shmid, (error_name (errno)));
  425.     fflush (stderr);
  426.     sleep (10);
  427.     kill (boss_pid, SIGCONT);
  428.     exit (1);
  429.   }
  430. #ifdef DEBUG_1
  431.   printf ("%s (start_drones): Attached shared memory at address = 0x%lx.\n",
  432.       arguments.program_name, ((long) shared_memory));
  433.   fflush (stdout);
  434. #endif
  435.   posix_signal (SIGINT, SIG_IGN);
  436.   posix_signal (SIGQUIT, SIG_IGN);
  437.   posix_signal (SIGHUP, shutdown);
  438.   posix_signal (SIGTERM, shutdown);
  439.  
  440.   gc_buffers = ((struct buffer_info *)
  441.         (shared_memory + (arguments.nbuf * arguments.bufsiz)));
  442.   gc_drones = ((struct drone_info *) (gc_buffers + arguments.nbuf));
  443.   drone_version = ((unsigned long *) (gc_drones + arguments.tdron));
  444.   wait_mask = (drone_version + 1);
  445.   if ((* drone_version) != ((unsigned long) DRONE_VERSION_NUMBER))
  446.   {
  447.     fprintf (stderr,
  448.          "%s (start_drones): stored drone version != drone version.\n",
  449.          arguments.program_name);
  450.     fprintf (stderr, "\t*drone_version = %ld; DRONE_VERSION_NUMBER = %ld.\n",
  451.          (* drone_version), ((unsigned long) DRONE_VERSION_NUMBER));
  452.     fflush (stderr);
  453.     kill (boss_pid, SIGCONT);
  454.     exit (1);
  455.   }
  456.  
  457.   for (counter = 1, drone = (gc_drones + (arguments.sdron + 1));
  458.        counter < arguments.ndron;
  459.        counter++, drone ++)
  460.   {
  461.     if ((cpid = (fork ())) == 0)
  462.     {
  463.       drone->DRONE_PPID = my_pid;
  464.       process_requests (drone);
  465.       /*NOTREACHED*/
  466.     }
  467.     else if (cpid == -1)
  468.     {
  469.       fprintf (stderr,
  470.            "%s (start_drones): fork failed; errno = %s.\n",
  471.            arguments.program_name, (error_name (errno)));
  472.       fflush (stderr);
  473.     }
  474.   }
  475.   drone = (gc_drones + arguments.sdron);
  476.   drone->DRONE_PPID = boss_pid;
  477.   /* This is non-portable behavior to prevent zombies from being created. */
  478.   if (arguments.ndron != 1)
  479.     posix_signal (SIGCHLD, SIG_IGN);
  480.   process_requests (drone);
  481.   /*NOTREACHED*/
  482. }
  483.  
  484. void
  485. DEFUN (main, (argc, argv), int argc AND char ** argv)
  486. {
  487.   int count, nargs;
  488.   static char err_buf[1024];
  489. #if defined(DEBUG) || defined(DEBUG_1) || defined(DEBUG_2)
  490.   static char out_buf[1024];
  491.  
  492.   setvbuf (stdout, &out_buf[0], _IOFBF, (sizeof (out_buf)));
  493. #endif
  494.   setvbuf (stderr, &err_buf[0], _IOFBF, (sizeof (err_buf)));
  495.  
  496. #ifdef DEBUG_2
  497.   printf ("%s (main): Arguments =\n", argv[0]);
  498.   for (count = 1; count < argc; count++)
  499.     printf ("\t%s\n", argv[count]);
  500.   fflush (stdout);
  501. #endif
  502.  
  503.   nargs = ((sizeof (command_line)) / (sizeof (struct argdesc)));
  504.   boss_pid = (getppid ());
  505.   if (argc != nargs)
  506.   {
  507.     fprintf (stderr,
  508.          "%s (main): Wrong number of arguments (got %d, expected %d).\n",
  509.          argv[0], (argc - 1), (nargs - 1));
  510.     fflush (stderr);
  511.     kill (boss_pid, SIGCONT);
  512.     exit (1);
  513.   }
  514.   for (count = 0; count < nargs; count++)
  515.   {
  516.     if (command_line[count].format == STRING_FMT)
  517.       (* ((char **) command_line[count].location)) = argv[count];
  518.     else
  519.       sscanf (argv[count],
  520.           command_line[count].format,
  521.           command_line[count].location);
  522.   }
  523.  
  524. #ifdef DEBUG_2
  525.   printf ("%s (main): Parsed arguments =\n", argv[0]);
  526.   for (count = 0; count < nargs; count++)
  527.   {
  528.     if (command_line[count].format == STRING_FMT)
  529.       printf ("\t%s\t= %s\n",
  530.           command_line[count].name,
  531.           (* ((char **) (command_line[count].location))));
  532.     else
  533.       printf ("\t%s\t= %d\n",
  534.           command_line[count].name,
  535.           (* ((int *) (command_line[count].location))));
  536.   }
  537.   fflush (stdout);
  538. #endif
  539.  
  540.   start_drones ();
  541.   /*NOTREACHED*/
  542. }
  543.  
  544. #define MAIN main
  545.  
  546. #endif /* HAVE_SYSV_SHARED_MEMORY */
  547.  
  548. #ifndef MAIN
  549.  
  550. void
  551. DEFUN (main, (argc, argv), int argc AND char ** argv)
  552. {
  553.   fprintf (stderr, "%s: Not implemented.\n", (argv[0]));
  554.   fflush (stderr);
  555.   exit (1);
  556. }
  557.  
  558. #endif /* MAIN */
  559.